package com.owlsmart.bluecat.activity;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.owlsmart.bluecat.R;
import com.owlsmart.bluecat.adapter.HotMovieAdapter;
import com.owlsmart.bluecat.api.Api;
import com.owlsmart.bluecat.api.ApiConfig;
import com.owlsmart.bluecat.api.TitleCallback;
import com.owlsmart.bluecat.entity.BaseSubjects;
import com.owlsmart.bluecat.entity.Subjects;
import com.owlsmart.bluecat.listener.OnItemChildClickListener;

import java.util.HashMap;
import java.util.List;

public class HotMovieActivity extends BaseActivity implements OnItemChildClickListener {

    private static final String TAG = "HotMovieActivity";

    private List<Subjects> mData;

    private RecyclerView recyclerView;

    private HotMovieAdapter mHotMovieAdapter;

    // 全局布局管理器，兼容播放器的
    private LinearLayoutManager linearLayoutManager;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mHotMovieAdapter.setDatas(mData);
                    mHotMovieAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };


    @Override
    protected int initLayout() {
        return R.layout.activity_hot_movie;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recycleView_hot_movie);
    }

    @Override
    protected void initDate() {

        //getActivity()获取父控件（fragment获取父activity控件对象）
        linearLayoutManager = new GridLayoutManager(this,3);
        // // 创建布局管理器
        // GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        // 设置方向 水平还是垂直 androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        // 设置布局管理器
        recyclerView.setLayoutManager(linearLayoutManager);

        //创建适配器
        mHotMovieAdapter = new HotMovieAdapter(this);
        // 渲染
        recyclerView.setAdapter(mHotMovieAdapter);
        getBaseSubjects();
    }

    // private void getBaseSubjects() {
    //     List<BaseSubjects> list = response.getList();
    //                     if (list != null && list.size() > 0) {
    //                         datas = list;
    //                         mHandler.sendEmptyMessage(0);
    //                     }
    // }

    private void getBaseSubjects() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("type", "movie");
        params.put("sort", "recommend");
        params.put("tag", "%E7%83%AD%E9%97%A8");
        params.put("page_limit", "102");
        params.put("page_start", "0");
        Api.assembleDoubanParams(ApiConfig.END_MOVIE, params).getRequest(this, new TitleCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.d(TAG, "请求返回值" + res);
                BaseSubjects baseSubjects = new Gson().fromJson(res, BaseSubjects.class);
                Log.d(TAG, "映射返回值结果 = " + baseSubjects.getSubjects().get(0).getCover());
                if (baseSubjects != null && baseSubjects.getSubjects().size() > 0) {
                    List<Subjects> subjectsLists = baseSubjects.getSubjects();
                    if (subjectsLists != null && subjectsLists.size() > 0) {
                        mData = subjectsLists;
                        Log.d(TAG, "mData = " + mData.get(0).getCover());
                        mHandler.sendEmptyMessage(0);

                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
            }
        });
    }

    @Override
    public void onItemChildClick(int position) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}